package com.two.service.caipin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.two.common.entity.*;
import com.two.common.util.CommonResult;
import com.two.service.caipin.dao.LeiDao;
import com.two.service.caipin.dao.MenuDao;
import com.two.service.caipin.service.CaipinService;

import com.two.service.caipin.vo.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CaipinServiceServiceImpl implements CaipinService {
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private LeiDao leiDao;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public CommonResult fingall(Integer currentPage,Integer pageSize) {
        Page<Menu> page=new Page<>(currentPage,pageSize);
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        queryWrapper.eq("tai",0);
        queryWrapper.ge("number",1);
        menuDao.selectPage(page,queryWrapper);
        return new CommonResult(2000,"查询成功",page);
    }

    @Override
    public int delectByidq(String mid) {
        QueryWrapper<Menu> queryWrapper1=new QueryWrapper<>();
        //把字段是mid=1的tai字段修改为1
        queryWrapper1.eq("mid",mid);
        int delete = menuDao.delete(queryWrapper1);
        System.out.println(delete);
        return delete;
    }

    @Override
    public int update(Menu menu) {
        int i = menuDao.updateById(menu);
        return i;
    }
    //添加
    @Override
    public int insertcai(Menu menu) {
        int insert = menuDao.insert(menu);
        System.out.println(menu);
        return insert;
    }

    //模糊查询
    @Override
    public CommonResult moselect(String disName, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Menu> mohucaiall1 = menuDao.mohucaiall(disName);
        PageInfo<Menu> pageInfo=new PageInfo<>(mohucaiall1);
        System.out.println("===============>"+disName);
        return new CommonResult(2000,"模糊查询成功",pageInfo);
    }

    //待审核
    @Override
    public CommonResult shenselect(Integer currentPage, Integer pageSize,String token, RestaurantVo restaurantVo) {
        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Gson gson = new Gson();
        UserAndPermissions userAndPermissions = gson.fromJson(s, UserAndPermissions.class);
        User user = userAndPermissions.getUser();
        Integer roleid = user.getRoleid();
        PageHelper.startPage(currentPage,pageSize);
        List<Menu> list = menuDao.selectall(restaurantVo);
        PageInfo pageInfo = new PageInfo(list);
        Map map=new HashMap();
        map.put("list",pageInfo);
        map.put("roleid",roleid);
        return new CommonResult(2000,"查询成功",map);
    }

    //菜品分类
    @Override
    public CommonResult lei(String mid) {
        List<Menu> menus = menuDao.selectLei(mid);
        return new CommonResult(2000,"查询成功",menus);
    }

    //查询收藏
    @Override
    public PageInfo<Menu> selectAll(Integer currentPage, Integer pageSize, Integer userid, CollectVo collectVo) {
        PageHelper.startPage(currentPage,pageSize);
        List<Menu> list = menuDao.selectAll(userid,collectVo);
        PageInfo<Menu> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    @Override
    public CommonResult xiu(String usermonicker, String id) {
        Menu menu=new Menu();
        menu.setMid(id);
        menu.setStatus(0);
        menu.setUsermonicker(usermonicker);
        menuDao.updateById(menu);
        System.out.println();
        return new CommonResult(2000,"修改成功",null);
    }

    @Override
    public CommonResult bo(String usermonicker, BohuiVo bohuiVo,String id) {
        Menu menu=new Menu();
        menu.setUsermonicker(usermonicker);
        menu.setBohui(bohuiVo.bohui);
        menu.setStatus(1);
        UpdateWrapper<Menu> updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("mid",id);
        int update = menuDao.update(menu, updateWrapper);
        return new CommonResult(2000,"修改成功",null);
    }

    @Override
    public CommonResult leiDelete(DelectLeiVo delectLeiVo) {
        Restaurant restaurant = delectLeiVo.getRestaurant();
        Integer rid = restaurant.getRid();
        Integer cid = delectLeiVo.getCid();
        return new CommonResult(5000,"删除失败",null);
    }

    @Override
    public CommonResult leicname(Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<XicanVo> leicname = menuDao.leicname();
        PageInfo<XicanVo> cookstylePageInfo = new PageInfo<>(leicname);
        if (leicname!=null){
           return new CommonResult(2000,"查询成功",cookstylePageInfo);
       }
        return new CommonResult(2000,"查询失败",null);
    }

    @Override
    public CommonResult caicname(Integer cid,Integer rid) {
        if (cid==8){
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("cid",cid);
            queryWrapper.eq("rid",rid);
            queryWrapper.eq("status",2);

            List list = menuDao.selectList(queryWrapper);
            return new CommonResult(2000,"查询成功",list);
        }
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("cid",cid);
        queryWrapper.eq("rid",rid);
        List list = menuDao.selectList(queryWrapper);
        if (list!=null){
            return new CommonResult(2000,"查询成功",list);
        }
        return new CommonResult(5000,"查询失败",null);
    }

    @Override
    public CommonResult updateleiname(Integer cid,String style) {
        Cookstyle cookstyle=new Cookstyle();
        cookstyle.setCid(cid);
        cookstyle.setStyle(style);
        int i = leiDao.updateById(cookstyle);
        if (i>0){
            return new CommonResult(2000,"修改成功",i);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    @Override
    public CommonResult allxi() {
        QueryWrapper allxi1=new QueryWrapper();
        List cookstyle = leiDao.selectList(allxi1);
        if (cookstyle!=null){
            return new CommonResult(2000,"查询成功",cookstyle);
        }
        return new CommonResult(5000,"查询失败",null);
    }

    @Override
    public CommonResult cidName(Integer rid) {

        List<Menu> list = menuDao.cidName(rid);
        return new CommonResult(2000,"查询成功",list);
    }

    @Override
    public CommonResult ridallcaipin(Integer rid,Integer cid,String dishname,String usermonicker) {
        /*QueryWrapper<Menu> queryWrapper2=new QueryWrapper<>();
        queryWrapper2.eq("rid",rid);
        queryWrapper2.like(StringUtils.hasLength(dishname),"dish_name",dishname);
        queryWrapper2.like(!cid.equals(0),"cid",cid);
        if(cid!=0){
            queryWrapper2.like("cid",cid);
        }
       // queryWrapper2.orderByAsc(StringUtils.hasLength(orderby),orderby);
        List<Menu> list2 = menuDao.selectList(queryWrapper2);
        return new CommonResult(2000,"查询成功",list2);*/
        List<MenuPo> list2=menuDao.menupo(rid,cid,dishname,usermonicker);
        System.out.println("===========>"+list2);
        return new CommonResult(2000,"查询成功",list2);
    }

    @Override
    public CommonResult selectlei(XicanVo xicanVo) {
        QueryWrapper<Menu> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("cid",xicanVo.getCid());
        queryWrapper3.eq("rid",xicanVo.getRid());
        List<Menu> list = menuDao.selectList(queryWrapper3);
        if (list!=null){
            return new CommonResult(2000,"查询成功",list);
        }
        return new CommonResult(5000,"查询失败",null);
    }

    @Override
    public CommonResult selectcaiall(Integer currentPage,Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<Menu> selectcaiall = menuDao.selectcaiall();
        PageInfo<Menu> pageInfo=new PageInfo<>(selectcaiall);
        if (selectcaiall!=null){
            return new CommonResult(2000,"查询成功",pageInfo);
        }
        return new CommonResult(5000,"查询失败",null);
    }

    @Override
    public CommonResult insertcaixi(Cookstyle cookstyle) {
        int insert = leiDao.insert(cookstyle);
        if (insert!=0){
            return new CommonResult(2000,"添加成功",insert);
        }
        return new CommonResult(5000,"添加失败",null);
    }

    @Override
    public CommonResult updatecaixi(Cookstyle cookstyle) {
        int i = leiDao.updateById(cookstyle);
        if (i!=0){
            return new CommonResult(2000,"修改成功",i);
        }
        return new CommonResult(5000,"修改失败",null);
    }

    @Override
    public Menu goods(String mid) {
        QueryWrapper queryWrapper3=new QueryWrapper();
        queryWrapper3.eq("mid",mid);
        Menu menu = menuDao.selectOne(queryWrapper3);
        if (menu!=null){
            return menu;
        }
        return null;
    }

    @Override
    public CommonResult mselect(String mid,Integer rid) {
        QueryWrapper queryWrapper4=new QueryWrapper();
        queryWrapper4.eq("mid",mid);
        Menu menu = menuDao.selectOne(queryWrapper4);
        if (menu!=null){
            return new CommonResult(2000,"查询成功",menu);
        }
        return new CommonResult(5000,"查询失败",null);
    }

    @Override
    public List<Menu> collectSelect(Integer userid) {
        List<Menu> list = menuDao.collectSelect(userid);
        return list;
    }
}












